package com.gss.util.common;

import java.util.Date;
import java.util.Set;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.gss.domain.common.CommonDetailsDTO;


public class ApplicationUtilities 
{
	public static void populateCommonDetails(CommonDetailsDTO commonDetailsDTO,boolean isNew){
		 UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		 if(isNew){
			 commonDetailsDTO.setVersion(1);
			 commonDetailsDTO.setCreatedDate(new Date());
			 commonDetailsDTO.setStrCreatedAuthor(userDetails.getUsername());
		 }else{
			 commonDetailsDTO.setVersion(commonDetailsDTO.getVersion()+1);
			 commonDetailsDTO.setUpdatedDate(new Date());
			 commonDetailsDTO.setStrUpdatedAuthor(userDetails.getUsername());
		 }
	}
	public static boolean checkIfExisting(Set<String> setToCheck, String valueToCheck){
		valueToCheck = valueToCheck.trim().toLowerCase();
		boolean isPresent = false;
		for(String existingValue : setToCheck){
			existingValue = existingValue.trim().toLowerCase();
			if(existingValue.equalsIgnoreCase(valueToCheck)){
				isPresent = true;
				break;
			}
		}
		return isPresent;
	}
}
